<?php
class BaseController extends CController
{
	public function __construct($id,$module=null){
		Yii::app()->name = Yii::app()->params['sitename']!=NULL ? Yii::app()->params['sitename'] : Yii::app()->name;
		parent::__construct($id,$module);
		// If there is a post-request, redirect the application to the provided url of the selected language
		if(isset($_POST['language'])) {
			$lang = $_POST['language'];
			$MultilangReturnUrl = $_POST[$lang];
			$this->redirect($MultilangReturnUrl);
		}
		// Set the application language if provided by GET, session or cookie
		if(isset($_GET['language'])) {
			Yii::app()->language = $_GET['language'];
			Yii::app()->user->setState('language', $_GET['language']);
			$cookie = new CHttpCookie('language', $_GET['language']);
			$cookie->expire = time() + (60*60*24*365); // (1 year)
			Yii::app()->request->cookies['language'] = $cookie;
		}
		else if (Yii::app()->user->hasState('language'))
			Yii::app()->language = Yii::app()->user->getState('language');
		else if(isset(Yii::app()->request->cookies['language']))
			Yii::app()->language = Yii::app()->request->cookies['language']->value;
	}
	public function debug($v){
		echo '<br/><br/><br/><pre>';
		print_r($v);
		echo '</pre><br/>';
	}
	public function createMultilanguageReturnUrl($lang='en'){
		if (count($_GET)>0){
			$arr = $_GET;
			$arr['language']= $lang;
		}
		else
			$arr = array('language'=>$lang);
		return $this->createUrl('', $arr);
	}
	// флеш-нотис пользователю
	public function setNotice($message)
	{
		return Yii::app()->user->setFlash('notice', $message);
	}

	// флеш-ошибка пользователю
	public function setError($message)
	{
		return Yii::app()->user->setFlash('error', $message);
	}
	//
	public function material_image($id, $image, $width='150', $path = NULL, $class='material_img')
	{
		$image = str_replace('/upload/', '', $image);
		if(strpos($image, '/')){
			$path = substr($image, 0, strpos($image, '/'));
		}
		
		if (!empty( $path ) ) {
			$src = '/upload/' . $path . '/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/' . $path . '/';
		} else {
			$src = '/upload/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/';
		}
		
		$src = '/upload/';
		if ($width<=50){
			$resize_w = 50;
			$resize_path = $path.'l/';
			$src .= 'l/';
		} elseif ($width<=150){
			$resize_w = 150;
			$resize_path = $path.'m/';
			$src .= 'm/';
		} else {
			$resize_path = $path;
		}
		
		if (isset ( $image ) && file_exists ( $path . $image ) && $image != NULL) {
			if (!file_exists ( $resize_path . $image )) {
				if(!file_exists($resize_path)){
					mkdir($resize_path, 0755, true);
				}
				$simage = new SimpleImage();
				$simage->load($path.$image);
				$simage->resizeToWidth($resize_w);
				$simage->save($resize_path . $image);
			}
			return CHtml::image ( $src . $image, $id, array (
					'width' => $width,
					'class' => $class
			));
		} else {
			return CHtml::image ( Yii::app ()->getBaseUrl ( true ) . '/upload/noimage.gif', 'Нет картинки', array (
					'width' => $width,
					'class' => $class
			));
		}
	}
	public function material_image_src($id, $image, $width='150', $path, $class='material_img')
	{
		if (!empty( $path ) )
		{
			$src = '/upload/' . $path . '/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/' . $path . '/';
		} else {
			$src = '/upload/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/';
		}
		if ($width<=50){
			$resize_w = 50;
			$resize_path = $path.'l/';
			$src .= 'l/';
		} elseif ($width<=200){
			$resize_w = 200;
			$resize_path = $path.'m/';
			$src .= 'm/';
		} else {
			$resize_path = $path;
		}
		if (isset ( $image ) && file_exists ( $path . $image ) && $image != NULL) {
			if (!file_exists ( $resize_path . $image )) {
				if(!file_exists($resize_path)){
					mkdir($resize_path, 0755, true);
				}
				$simage = new SimpleImage();
				$simage->load($path.$image);
				$simage->resizeToWidth($resize_w);
				$simage->save($resize_path . $image);
			}

			return $src;
		} else {
			return Yii::app ()->getBaseUrl ( true ) . '/upload/noimage.gif';
		}
	}
}
